<?php
namespace Home\Controller;
use Think\Controller;
class PrivilegeController extends BaseController {
    public function index(){
        $privilegeModel = D('Privilege');
        $pridata = $privilegeModel->getTree();
        $count = $privilegeModel->count();
        $this->assign('count',$count);
        $this->assign('pridata',$pridata);
        $this->display();
    }
    public function add(){
        $privilegeModel = D('Privilege');
        if(IS_AJAX){
            $data = $_POST;
            if($privilegeModel->add($data)){
                return show(1,'添加成功');
            }else{
                return show(0,'添加失败');
            }
        }else{
            $pridata = $privilegeModel->getTree();
            $this->assign('pridata',$pridata);
            $this->display();
        }
    }
    public function edit(){
        $id = I('get.id');
        $p = I('get.p');  //编辑
        if(IS_AJAX){
            $data = $_POST;
            if(M('Privilege')->save($data)){
                return show(1,'编辑成功');
            }else{
                return show(0,'编辑失败');
            }
        }else{
            //取父类(也就是上级)
            $parentModel = D('Privilege');
            $parentData = $parentModel->getTree();
            $children = $parentModel->getChildren($id);
            $this->assign(array(
                    'parentData'=>$parentData,
                    'children'=>$children,
                )
            );
            //先取出自己的基本数据
            $model = M('Privilege');
            $data = $model->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }
    public function del(){
        $id = I('post.id',0);  //如果没有参数就是默认值0
        $ids = D("Privilege")->delete($id);  //删除之前有钩子函数
        if($ids !== false){//删除成功
            return show(1,'删除成功!');
        }else{
            return show(0,'删除失败!');
        }
    }

}




















